package com.powerbank.devicecenter.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

/**
 * 设备状态消息监听器
 */
@Slf4j
@Component
public class DeviceStatusMessageListener implements IMqttMessageListener {

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String payload = new String(message.getPayload());
        log.info("接收到设备状态消息: topic={}, message={}", topic, payload);
        
        // 解析设备编号
        String deviceCode = parseDeviceCodeFromTopic(topic);
        if (deviceCode != null) {
            // 处理设备状态更新
            // TODO: 实现设备状态处理逻辑
        }
    }

    private String parseDeviceCodeFromTopic(String topic) {
        // 从topic中解析设备编号: powerbank/device/{deviceCode}/status
        String[] parts = topic.split("/");
        if (parts.length >= 4) {
            return parts[2];
        }
        return null;
    }
}